import { defineStore } from 'pinia';
import { ref } from 'vue';
import operateWhiteBoard from '@/apis/operate-white-board.ts';

export const useBoardStore = defineStore('board', () => {
  const currentBoardId = ref('');
  const currentBoardName = ref('');

  function setCurrentBoardId(boardId: string) {
    currentBoardId.value = boardId;
  }
  function setCurrentBoardName(boardName: string) {
    currentBoardName.value = boardName;
  }

  // 获取白板详细信息 请求操作
  async function getBoardDetails(boardId: string) {
    try {
      const res = await operateWhiteBoard.getBoardDetails({
        boardId,
      });
      return res?.data;
    } catch (e) {
      console.error(e);
    }
  }
  // 重命名白板 请求操作
  async function renameBoard(boardId: string, boardName: string) {
    try {
      await operateWhiteBoard.renameBoard({
        boardId,
        newName: boardName,
      });
    } catch (e) {
      console.error(e);
    }
  }
  // 删除白板（进入回收站） 请求操作
  async function removeBoard(boardId: string) {
    try {
      await operateWhiteBoard.removeBoard({
        boardId,
      });
    } catch (e) {
      console.error(e);
    }
  }
  // 恢复白板
  async function recoverBoard(boardId: string) {
    try {
      await operateWhiteBoard.recoverBoard({
        boardId,
      });
    } catch (e) {
      console.error(e);
    }
  }
  // 彻底删除白板
  async function cleanOutBoard(boardId: string) {
    try {
      await operateWhiteBoard.cleanOutBoard({
        boardId,
      });
    } catch (e) {
      console.error(e);
    }
  }
  // 修改白板成员的角色
  async function changeBoardMemberRole(memberId: string, roleCode: string) {
    try {
      return await operateWhiteBoard.changeBoardMemberRole({
        boardId: currentBoardId.value,
        boardMemberId: memberId,
        newRoleCode: roleCode,
      });
    } catch (e) {
      console.error(e);
    }
  }
  // 获取白板邀请链接
  async function getBoardInviteUrl(assignRoleCode: string) {
    try {
      return await operateWhiteBoard.getBoardInviteUrl({
        boardId: currentBoardId.value,
        assignRoleCode,
      });
    } catch (e) {
      console.error(e);
    }
  }
  // 重置白板邀请链接
  async function resetBoardInviteUrl(assignRoleCode: string) {
    try {
      return await operateWhiteBoard.resetBoardInviteUrl({
        boardId: currentBoardId.value,
        assignRoleCode,
      });
    } catch (e) {
      console.error(e);
    }
  }
  // 获取白板邀请详情
  async function getBoardInviteDetails(inviteCode: string) {
    try {
      return await operateWhiteBoard.getBoardInviteDetails({
        inviteCode,
      });
    } catch (e) {
      console.error(e);
    }
  }
  // 使用邀请码进入白板
  async function joinInBoardByInviteCode(inviteCode: string) {
    try {
      return await operateWhiteBoard.joinInBoardByInviteCode({
        inviteCode,
      });
    } catch (e) {
      console.error(e);
    }
  }

  return {
    currentBoardId,
    currentBoardName,
    setCurrentBoardId,
    setCurrentBoardName,
    getBoardDetails,
    renameBoard,
    removeBoard,
    recoverBoard,
    cleanOutBoard,
    changeBoardMemberRole,
    getBoardInviteUrl,
    resetBoardInviteUrl,
    getBoardInviteDetails,
    joinInBoardByInviteCode,
  };
});
